#ifndef PLURKAPI_H
#define PLURKAPI_H

#include <QObject>
#include <QScriptEngine>
#include "oauth.h"

#define PLURK_HOST "http://www.plurk.com"
#define PLURK_AUTHORIZE PLURK_HOST "/m/authorize?oauth_token="
#define PLURK_REQUEST_TOKEN PLURK_HOST "/OAuth/request_token"
#define PLURK_ACCESS_TOKEN PLURK_HOST "/OAuth/access_token"
#define PLURK_UNREAD_COUNT PLURK_HOST "/APP/Polling/getUnreadCount"
#define PLURK_GET_PLURKS PLURK_HOST "/APP/Timeline/getPlurks"
#define PLURK_GET_RESPONSE PLURK_HOST "/APP/Responses/get"
#define PLURK_ADD_PLURK PLURK_HOST "/APP/Timeline/plurkAdd"
#define PLURK_ADD_RESPONSE PLURK_HOST "/APP/Responses/responseAdd"

class PlurkAPI : public QObject
{
    Q_OBJECT
public:
    PlurkAPI();
    PlurkAPI(const PlurkAPI &) { }
//    explicit PlurkAPI(QObject *parent = 0);
    ~PlurkAPI() { }
    void setConsumerKey(QString consumerKey);
    void setConsumerSecret(QString consumerSecret);
    void setAppKey(QString appKey);
    void setAppSecret(QString appSecret);
    QString consumerKey();
    QString consumerSecret();
    QString appKey();
    QString appSecret();
    Q_INVOKABLE void initRequestToken();
    void startAccessToken(QString verifier);
    Q_INVOKABLE void GetUnreadCount();
    Q_INVOKABLE void GetPlurks();
    Q_INVOKABLE void AbortPlurks();
    Q_INVOKABLE void GetResponses(int id);
    Q_INVOKABLE void AbortResponses();
    Q_INVOKABLE void PlurkAdd(const QString &content);
    Q_INVOKABLE void ResponseAdd(int id, const QString &content);

signals:
    void openAuthUrl(const QUrl &url);
    void finishOAuth();
    void unreadCount(const QVariant &value);
    void plurksReceived(const QVariant &value);
    void responsesReceived(const QVariant &value);
    void plurkRequestError();
    void plurkAdded();
    void responseAdded();
    void plurkError();

private slots:
    void performAuth(const QString &token);
    void finishAccess();
    void appRequestFinish(const QUrl &url, const QString &data);
    void appRequestError(const QUrl &url, const QString &data);

private:
    QString _consumerKey;
    QString _consumerSecret;
    QString _appKey;
    QString _appSecret;
    QString _tokenVerifier;
    OAuth   oauth;
    QNetworkReply *plurksReply;
    QNetworkReply *responsesReply;
};

#endif // PLURKAPI_H
